package com.ztz.mynewdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.ztz.mynewdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CarouselActivity extends BaseActivity {
    public static final String WEB_VIEW = "webview";
    @BindView(R.id.wb_car)
    WebView wbCar;
    String webUrl;
    @BindView(R.id.content_progress)
    ProgressBar contentProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_carschel);
        ButterKnife.bind(this);
        webUrl = getIntent().getStringExtra(WEB_VIEW);
        WebSettings webSettings = wbCar.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setAppCacheEnabled(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        wbCar.setWebViewClient(new WebViewClient());
        setToolbar("详情");
        wbCar.loadUrl(webUrl);
        wbCar.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //获取webview图片，长按保存
                WebView.HitTestResult result = ((WebView) v).getHitTestResult();
                if (null == result)
                    return false;
                int type = result.getType();
                if (type == WebView.HitTestResult.UNKNOWN_TYPE)
                    return false;
                switch (type) {
                    case WebView.HitTestResult.IMAGE_TYPE:
                        String saveImgUrl = result.getExtra();
                        Intent i = new Intent(CarouselActivity.this, PhotoAcitivy.class);
                        i.putExtra(PhotoAcitivy.PHOTO, saveImgUrl);
                        startActivity(i);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
        wbCar.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100)
                {
                    contentProgress.setVisibility(View.GONE);
                }
                else {
                    contentProgress.setVisibility(View.VISIBLE);
                    contentProgress.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
    }
}
